package com.wenchao.quickstart.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wenchao.quickstart.R;
import com.wenchao.quickstart.utils.glide.GlideCircleTransfromUtil;

/**
 * 图片加载器
 */
public class ImageLoader {

    private static final String TAG = ImageLoader.class.getSimpleName();
    public static int PLACE_PIC_ID = R.mipmap.ic_place;

    public static void display(Context context, ImageView imageView, String url) {
        display(context, imageView, url, 0, 0);
    }

    public static void display(Context context, ImageView imageView, int resId) {
        display(context, imageView, resId, 0, 0);
    }

    public static void display(Context context, ImageView imageView, String url, int place, int error) {
        if (context == null || imageView == null) {
            throw new NullPointerException("context or imageView is null");
        }
        Glide.with(context).load(url)
                .placeholder(place == 0 ? PLACE_PIC_ID : place)
                .error(error == 0 ? PLACE_PIC_ID : error)
                .into(imageView);
    }

    public static void display(Context context, ImageView imageView, int resId, int place, int error) {
        if (context == null || imageView == null) {
            throw new NullPointerException("context or imageView is null");
        }
        Glide.with(context).load(resId)
                .placeholder(place == 0 ? PLACE_PIC_ID : place)
                .error(error == 0 ? PLACE_PIC_ID : error)
                .into(imageView);
    }

    public static void displayCircle(Context context, ImageView imageView, String url) {
        displayCircle(context, imageView, url, 0, 0);
    }

    public static void displayCircle(Context context, ImageView imageView, String url, int place, int error) {
        if (context == null || imageView == null) {
            throw new NullPointerException("context or imageView is null");
        }
        Glide.with(context).load(url)
                .placeholder(place == 0 ? PLACE_PIC_ID : place)
                .error(error == 0 ? PLACE_PIC_ID : error)
                .transform(new GlideCircleTransfromUtil(context))
                .into(imageView);
    }

    public static void display2Bitmap(Context context, ImageView imageView, String url){
        if (context == null || imageView == null) {
            throw new NullPointerException("context or imageView is null");
        }
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        imageView.setImageBitmap(resource);
                    }
                });
    }

    public static void targetDisplayNoCache(Context context, String url, final OnBitmapListener bitmapListener) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.NONE);
        options.centerCrop();
        options.placeholder(PLACE_PIC_ID);
        options.error(PLACE_PIC_ID);
        Glide.with(context).asBitmap().load(url).apply(options).into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                bitmapListener.returnBitmap(resource);
            }
        });
    }

    public interface OnBitmapListener {

        void returnBitmap(Bitmap bitmap);
    }

}
